

package com.gc.server.service.impl;
import org.springframework.transaction.annotation.Transactional;
import com.gc.server.vo.up.BookTypeUp;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.excel.context.AnalysisContext;
import java.io.InputStream;
import com.alibaba.excel.event.AnalysisEventListener;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import  com.gc.server.vo.down.BookTypeDown;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.net.URLEncoder;
import com.gc.server.vo.down.BookTypeDown;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;
import com.gc.server.dao.BookTypeDao;
import com.gc.server.entity.BookTypeEntity;
import com.gc.server.vo.BookTypeVo;
import com.gc.server.service.BookTypeService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Wed Nov 11 11:48:17 CST 2020
 */
@Service
public class BookTypeServiceImpl extends BaseServiceImpl<BookTypeEntity> implements BookTypeService {

    private BookTypeDao dao;


    @Autowired
    public void setDao(BookTypeDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }




    @Override
    public List<BookTypeVo> findVo(BookTypeVo vo){
        return dao.findVo(vo);
    }

    @Override
    public BookTypeVo getVo(Long id){
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(BookTypeVo vo){
        return dao.save(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(BookTypeVo vo){
        return dao.update(vo.getEntity());
    }



    @Override
    public void downLoad(HttpServletResponse response, List<BookTypeVo> list) {
        try {
            List<BookTypeDown> datas = list.stream().map(item -> new BookTypeDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel" );
            response.setCharacterEncoding("utf-8" );
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8" );
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx" );
            EasyExcel.write(response.getOutputStream(), BookTypeDown.class).sheet("模板" ).doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    @Override
    public void upload(InputStream file) {
        EasyExcel.read(file, BookTypeUp.class, new UploadDataListener()).sheet().doRead();
    }

    @Override
    public List<BookTypeEntity> findByIdx() {
        return dao.findByIdx();
    }

    class UploadDataListener extends AnalysisEventListener<BookTypeUp> {


        @Override
        @Transactional(rollbackFor = Exception.class)
        public void invoke(BookTypeUp upVo, AnalysisContext analysisContext) {
            saveVo(upVo.toVo());
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }

    }




    /**内部方法**/



    /**外部方法实现**/




}
